// pages/cart/index.js
// import {
//     showToast
// } from '../../utils/asyncWx.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cart: [],
        allChecked: false,
        totalNum: 0,
        totalPrice: 0,
    },
    onShow() {
        //获取缓存中的收获地址信息
        const address = wx.getStorageSync('address');
        //获取缓存中的购物车数据
        const cart = wx.getStorageSync('cart') || [];
        //计算全选
        //every 数组方法 会便利 会接收一个回调函数 每一个回调函数都返回true every方法的返回值为true只要有一个返回false 就返回一个false
        //空数组调用every返回true
        // const allChecked = cart.length ? cart.every(v => v.checked) : false;
        this.setData({
            address,
        })
        this.setCart(cart);
    },

    handleChooseAddress() {
        // 调用内置api wx.chooseAddress({success: (result) => {},}) 获取用户的收货地址
        wx.showModal({
            title: "提示",
            content: '月亮想访问你的地址',
            success(res) {
                if (res.confirm) {
                    wx.chooseAddress({
                        success: (address) => {
                            wx.setStorageSync('address', address)
                        },
                    })
                    console.log('用户点击确定')
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    handleItemChange(e) {
        const goods_id = e.currentTarget.dataset.id
        //    console.log(goods_id);
        //获取购物车数组
        let {
            cart
        } = this.data
        let index = cart.findIndex(v => v.goods_id === goods_id)
        //选中状态取反
        cart[index].checked = !cart[index].checked;
        this.setCart(cart)
    },
    //商品全选
    handleItemAllCheck() {
        let {
            cart,
            allChecked
        } = this.data;
        allChecked = !allChecked
        cart.forEach(v => v.checked = allChecked);
        this.setCart(cart)
    },
    //商品数量加减
    handleitemNum(e) {
        console.log(e);
        //获取传递过来的参数
        const {
            operation,
            id
        } = e.currentTarget.dataset
        console.log(operation, id);
        //获取购物车数组
        let {
            cart
        } = this.data;
        console.log(cart);
        //找到需要修改的商品的索引
        const index = cart.findIndex(v => v.goods_id === id);
        if (cart[index].num === 1 && operation == -1) {
            wx.showModal({
                title: '温馨提示',
                content: '您确定要把我删除吗😔？',
                cancelText:"我再想想",
                cancelColor:"#00a160",
                success: (res) => {
                    if (res.confirm) {
                        cart.splice(index, 1);
                        this.setCart(cart)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })

        } else {
            cart[index].num += operation;
            this.setCart(cart);
        }

    },
    //结算
    handlepay() {
        const {
            address,
            totalNum,
        } = this.data;
        if (!address.userName) {
            wx.showToast({
                title: '您还没有选择收获地址',
                icon:'none'
            })
            return;
        }
        if (totalNum===0) {
            wx.showToast({
                title: '您还没有选购商品',
                icon:'none'
            })
            return;
        }
        wx.navigateTo({
          url: '/pages/pay/index',
        })
    },
    setCart(cart) {

        let allChecked = true;
        let totalPrice = 0;
        let totalNum = 0;
        cart.forEach(v => {
            if (v.checked) {
                totalPrice += v.num * v.goods_price;
                totalNum += v.num
            } else {
                allChecked = false
            }
        });
        allChecked = cart.length != 0 ? allChecked : false
        this.setData({
            cart,
            allChecked,
            totalPrice,
            totalNum,
        });
        wx.setStorageSync('cart', cart)
    }
})